﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Unity;
using TestWpfPrism.Accessors;
using TestWpfPrism.Views;

namespace TestWpfPrism
{
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            var w = Container.Resolve<MainWindow>();
            return w;
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterSingleton<IStoreAccessor, StoreAccessor>();

            containerRegistry.RegisterForNavigation<FirstView>();
            containerRegistry.RegisterForNavigation<SecondView>();
        }

        protected override void ConfigureViewModelLocator()
        {
            // MyWindow|MyView => ViewModels/MyViewModel
            base.ConfigureViewModelLocator();

            ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
            {
                Type? result;
                var viewName = viewType.FullName;
                var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;

                // My => MyViewModel
                var viewModelName = $"{viewName}ViewModel, {viewAssemblyName}";
                result = Type.GetType(viewModelName);

                // MyView => MyViewModel
                if (result is null
                    && viewName!.EndsWith("View"))
                {
                    viewModelName = $"{viewName}Model, {viewAssemblyName}";
                    result = Type.GetType(viewModelName);
                }

                return result;
            });
        }
    }
}
